﻿using System;
using System . Collections . Generic;
using System . ComponentModel;
using System . Linq;
using System . Runtime . CompilerServices;
using System . Text;
using System . Threading . Tasks;
using Windows . Storage;

namespace WenceyWang . RichMan4L . App . Logic
{
	public class AppSettings : INotifyPropertyChanged
	{
		/// <summary>
		/// 指示强迫症模式是否开启
		/// </summary>
		public bool OCDMode
		{
			get
			{
				return ReadSettings ( nameof ( OCDMode ) , false );
			}
			set
			{
				SaveSettings ( nameof ( OCDMode ) , value );
				NotifyPropertyChanged ( );
			}
		}

		#region Sound
		
		/// <summary>
		/// 指示是否播放声音
		/// </summary>
		public bool PlaySound
		{
			get
			{
				return ReadSettings ( nameof ( PlaySound ) , false );
			}
			set
			{
				SaveSettings ( nameof ( PlaySound ) , value );
				NotifyPropertyChanged ( );
			}
		}
		
		/// <summary>
		/// 音乐音量
		/// </summary>
		public int MusicVolume
		{
			get
			{
				return ReadSettings ( nameof ( MusicVolume ) , 60 );
			}
			set
			{
				SaveSettings ( nameof ( MusicVolume ) , value );
				NotifyPropertyChanged ( );
			}
		}

		/// <summary>
		/// 效果音量
		/// </summary>
		public int EffectVolume
		{
			get
			{
				return ReadSettings ( nameof ( EffectVolume ) , 10 );
			}
			set
			{
				SaveSettings ( nameof ( EffectVolume ) , value );
				NotifyPropertyChanged ( );
			}
		}

		/// <summary>
		/// 通知音量
		/// </summary>
		public int NotificationVolume
		{
			get
			{
				return ReadSettings ( nameof ( NotificationVolume ) , 30 );
			}
			set
			{
				SaveSettings ( nameof ( NotificationVolume ) , value );
				NotifyPropertyChanged ( );
			}
		}

		#endregion

		/// <summary>
		/// 春季的长度
		/// </summary>
		public int SpringLenth
		{
			get
			{
				return ReadSettings ( nameof ( SpringLenth ) , 20 );
			}
			set
			{
				SaveSettings ( nameof ( SpringLenth ) , value );
				NotifyPropertyChanged ( );
			}
		}

		public int SummerLenth
		{
			get
			{
				return ReadSettings ( nameof ( SummerLenth ) , 20 );
			}
			set
			{
				SaveSettings ( nameof ( SummerLenth ) , value );
				NotifyPropertyChanged ( );
			}
		}

		public int AutumnLenth
		{
			get
			{
				return ReadSettings ( nameof ( AutumnLenth ) , 20 );
			}
			set
			{
				SaveSettings ( nameof ( AutumnLenth ) , value );
				NotifyPropertyChanged ( );
			}
		}

		public int WinterLenth
		{
			get
			{
				return ReadSettings ( nameof ( WinterLenth ) , 20 );
			}
			set
			{
				SaveSettings ( nameof ( WinterLenth ) , value );
				NotifyPropertyChanged ( );
			}
		}

		public ApplicationDataContainer RoamingSettings { get; set; }

		public AppSettings ( )
		{
			RoamingSettings = ApplicationData . Current . RoamingSettings;
		}

		private void SaveSettings ( string key , object value )
		{
			RoamingSettings . Values [ key ] = value;
		}

		private T ReadSettings<T> ( string key , T defaultValue )
		{
			if ( RoamingSettings . Values . ContainsKey ( key ) )
			{
				return ( T ) RoamingSettings . Values [ key ];
			}
			if ( null != defaultValue )
			{
				return defaultValue;
			}
			return default ( T );
		}

		public event PropertyChangedEventHandler PropertyChanged;

		protected void NotifyPropertyChanged ( [CallerMemberName]string propName = "" )
		{
			PropertyChanged?.Invoke ( this , new PropertyChangedEventArgs ( propName ) );
		}
	}
}
